<?php

class controller_users extends __registry_controller
{
    public $_ = array();
    public $_layout = NULL;
    public $_js = NULL;

    public $_content_top = NULL;
    public $_content_bot = NULL;
    public $_content_right = NULL;
    public $_content_left = NULL;
    public $_content_header = 'OPEN';
    public $_content_footer = NULL;
    public $_table = 'users';

    public function __construct()
    {	
		$this->_layout =NULL;
        parent::add_object(array(
            'handle_validate',
            'model_users',
            'handle_paging'
        ));
        $this->_js = array('jquery.validate.min');
	}
	
	public function index_action()
	{
		
		/* Check permission */
		$this->per_check_permission();
	   
       $this->_['com'] = $this->_seokey[1];
       
       /*****************pagination**********************/
       $limit = 2;
       $current = intval(end($this->_seokey)) !=0?intval(end($this->_seokey)):1;
       $total = $this->mdu_get_total($this->_table);
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_add'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('add');
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("index");
       $arrHtml = $this->view->view_get_block_html($html);
       $list_users = $this->mdu_get_list_user($limit,$current);
       if(!empty($list_users))
       {
            foreach($list_users as $user)
            {
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$user);
            
            }
       }
       $strHtmlContent = $this->view->view_assign_block($html, array('htmlCat'=>$strOkHtml));
	   return $strHtmlContent;
	}
    
    
	public function index_ajax_action()
	{
		/* Check permission */
		$this->per_check_permission();
		
        $this->_layout ='NO_LAYOUT';
        $active = isset($this->_seokey[2])?$this->_seokey[3]:null;
        $str_id = isset($this->_seokey[3])?$this->_seokey[4]:null;
        $done = $this->mdu_tool_func($this->_table, $this->model,$active,$str_id);
        if($done)
        {
            return 'ok';
        }else{
            return 'noOk';
        }
        
	}
    
    public function add_action(){
        
		/* Check permission */
		$this->per_check_permission();
		
		$this->_['label-error'] = '';
        $this->_['com'] = $this->_seokey[1];
        $this->_['error'] = '';
        $this->_layout = 'iframe';
        
        /***********saver du lieu******************/
        if(isset($_POST) && !empty($_POST)){
            if(isset($_POST['Email']) && $_POST['Email'] != '')
            {
                if($this->validate_email($_POST['Email']) && $_POST['Password'] == $_POST['conf_password'] && !empty($_POST['Password']))
                {
                    $data = array(
                        'Email'=>$_POST['Email'],
                        'Fullname'=>$_POST['Fullname'],
                        'Sex'=>$_POST['Sex'],
                        'YahooChat'=>$_POST['YahooChat'],
                        'Phone'=>$_POST['Phone'],
                        'Mobi'=>$_POST['Mobi'],
                        'CMND'=>$_POST['CMND'],
                        'Address'=>$_POST['Address'],
                        'Website'=>$_POST['Website'],
                        'Company'=>$_POST['Company'],
                        'Password'=>md5(md5($_POST['Password'])),
                        'PostDate'=>mktime()
                    );
                    if($_POST['Status'] == 'show')
                    {
                        $data['Status'] = 'true';
                    }else
                    {
                        $data['Status'] = 'false';
                    }
                    $table = 'user';
                    $this->mdu_insert_user($table, $data);
                    $id_last = $this->mdu_get_last_id();
                    
                    if(isset($_POST['group']) && !empty($_POST['group']))
                    {
                        $group = $_POST['group'];
                        $this->mdu_insert_group_user($id_last, $group);
                    }
                    
                    $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey['1']); 
                }else
                {
                    $this->_['dis'] = 'block';
                    $this->_['error'] = $this->_['error_password'];
                }
            }else{
                $this->_['error'] = $this->_['error_email'];
            }
        }
        
        /*******************list group*******************/
        $list_group = $this->mdu_get_group();
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';
        if(!empty($list_group))
        {
            foreach($list_group as $key=>$value)
            {
                $strOkHtml .= $this->view->view_assign(
                                                        $arrHtml['group'],
                                                        array(
                                                                'group_name'           => $value['GroupName'],
                                                                'group_id'             => $value['ID']
                                                        )
                                                    );
            }
        }
         /********************list sex*********************/
        $array_sex = array($this->_['nu'],$this->_['nam'],$this->_['no_sex']);
        $strOkHtml_sex = '';
        foreach($array_sex as $key=>$value )
        {
            $strOkHtml_sex .= $this->view->view_assign($arrHtml['sex'],array(
                                                                            'sex_name'           => $value,
                                                                            'sex_key'           => $key
                                                                        ));
        }
        $strHtmlContent = $this->view->view_assign_block(
                                                            $strHtml, 
                                                            array(
                                                                'list_group'=>$strOkHtml,
                                                                'list_sex'=>$strOkHtml_sex
                                                                )
                                                        );
        
        /**************add data************************/
        $array_form_data = array('Email' =>'email_db', 'Fullname'=>'full_name_db', 'YahooChat'=>'yahoo_db', 'Phone'=>'phone_db', 'Mobi'=>'mobile_db', 
                                    'CMND'=>'cmnd_db', 'Address'=>'address_db', 'Company'=>'company_db', 'Website'=>'website_db');
        foreach($array_form_data as $key=>$value){
            if(empty($_POST[$key]))
            {
                $this->_[$value] = '';
            }else{
                $this->_[$value] = $_POST[$key];
            }
        }
        if(!empty($this->_))
        {
            $strHtmlContent = $this->view->view_assign($strHtmlContent,$this->_);
        }

        return $strHtmlContent;
    }
    
    public function edit_action(){
		
		/* Check permission */
		$this->per_check_permission();
		
        $this->_['label-error'] = '';
        $this->_['error'] = '';
        $this->_layout = 'iframe';
        $id = $this->_seokey[3];
        $get_user = $this->mdu_get_user_from_id($id);
        /**********save du lieu************************/
        if(isset($_POST) && !empty($_POST)){
            if(isset($_POST['Email']) && $_POST['Email'] != '')
            {
                if($this->validate_email($_POST['Email']))
                {
                    $data = array(
                        'Email'=>$_POST['Email'],
                        'Fullname'=>$_POST['Fullname'],
                        'Sex'=>$_POST['Sex'],
                        'YahooChat'=>$_POST['YahooChat'],
                        'Phone'=>$_POST['Phone'],
                        'Mobi'=>$_POST['Mobi'],
                        'CMND'=>$_POST['CMND'],
                        'Address'=>$_POST['Address'],
                        'Company'=>$_POST['Company'],
                        'Website'=>$_POST['Website']
                    );
                    $data['Status'] = 'true';
                    
                    if(isset($_POST['Password']) && !empty( $_POST['Password']) &&  $_POST['Password'] ==  $_POST['conf_password']){
                        $data['Password'] = md5(md5($_POST['Password']));
                    }

                    $id_user = $_POST['id_user'];
                    $condition = "ID = " . $id_user;
                    $this->mdu_model_update_user($data, $condition);
                    
                    /****************save group*****************************/
                    $this->mdu_del_group_user($id_user);
                    if(isset($_POST['group']) && !empty($_POST['group']))
                    {
                        $group = $_POST['group'];
                        $this->mdu_insert_group_user($id_user, $group);
                    }
                    
                    $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey['1']); 
                }else{
                    $this->_['dis'] = 'block';
                    $this->_['label-error'] = $this->_['error_password'];
                }
            }else{
                $this->_['label-error'] = $this->_['error_email'];
            }
        }
        
        /*******************list group*******************/
        $list_group = $this->mdu_get_group();
        $list_user_group = $this->mdu_list_user_group($id);
        $array_list_user_group_id = array();
        
        if(!empty($list_user_group)){
            foreach($list_user_group as $value){
                $array_list_user_group_id[] = $value['ID'];
            }
        }
        
        $strHtml = $this->view->view_display('edit');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';

        if(!empty($list_group))
        {
            foreach($list_group as $key=>$value)
            {
                
                if(in_array($value['ID'], $array_list_user_group_id)){
                    $checked = 'checked=""';
                }else 
                    $checked = '';
                $strOkHtml .= $this->view->view_assign($arrHtml['group'],array(
                                                                            'group_name'           => $value['GroupName'],
                                                                            'group_id'             => $value['ID'],
                                                                            'checked'              => $checked
                                                                        ));
            }
        }
         /********************list sex*********************/
        $array_sex = array($this->_['nu'],$this->_['nam'],$this->_['no_sex']);
        $sex_user = $get_user['Sex'];
        $strOkHtml_sex = '';
        foreach($array_sex as $key=>$value )
        {
            if($key == $sex_user)
            {
                $selected = 'selected=""';
            }else 
                $selected = '';
            $strOkHtml_sex .= $this->view->view_assign($arrHtml['sex'],
                                                        array(
                                                                'sex_name'           => $value,
                                                                'sex_key'           => $key,
                                                                'selected'           => $selected
                                                        )
            );
        }
        /******************show data****************************/
        $this->_['email_db'] = $get_user['Email'];
        $this->_['full_name_db'] = $get_user['Fullname'];
        $this->_['yahoo_db'] = $get_user['YahooChat'];
        $this->_['phone_db'] = $get_user['Phone'];
        $this->_['mobile_db'] = $get_user['Mobi'];
        $this->_['cmnd_db'] = $get_user['CMND'];
        $this->_['address_db'] = $get_user['Address'];
        $this->_['company_db'] = $get_user['Company'];
        $this->_['website_db'] = $get_user['Website'];
        $this->_['password_db'] = '';
        $this->_['confirm_password_db'] = '';
        $this->_['id_user'] = $get_user['ID'];

        
        $arr_status = array('true'=>'true','false'=>'false');
        $strOkHtmlStatus = '';
        foreach($arr_status as $key=>$status)
        {
            $select_status = '';
            if($get_user['Status'] == $status)
            {
                $select_status = 'selected=""';
            }
            $strOkHtmlStatus .= '<option '.$select_status.' value="'.$key.'">'.$this->_[$status].'</option>';
        }
        $this->_['html_status'] = $strOkHtmlStatus;
        /******************end**********************************/
        $strHtmlContent = $this->view->view_assign_block($strHtml, 
                                                         array(
                                                                'list_group'=>$strOkHtml,
                                                                'list_sex'=>$strOkHtml_sex
                                                         ));

        return $strHtmlContent;
    }
}